<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/17
 * Time: 10:43
 */

namespace app\admin\controller\mall;


use app\admin\BaseController;
use app\admin\model\SpecItem;
use app\lib\exception\FailedException;
use app\Validate\AttrNameAddValidate;
use app\Validate\AttrValAddValidate;
use app\Validate\IdMustPositive;
use app\Validate\PageMustValidate;
use think\Db;
use think\Exception;
use think\Request;

class AttrVal extends BaseController
{


    /**
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     */
    public function index(Request $request)
    {
        (new PageMustValidate())->goCheck();
        $where = [];
        $post  = $request->post();
        $where = addWhere($post,['spec_item_value'=>''],$where,2);//关键字模糊搜索
        $count = (new SpecItem())->getCount($where);
        $where['page'] = $post['page'];
        $dataList =  (new \app\admin\model\SpecItem())->wechatListWith("specName",$where,"*","sort desc");
        return writeJson(200,['data'=>$dataList,'count'=>$count]);
    }

    /**
     * 属性值新增
     * spec_title
     * @param Request $request
     * @return \think\response\Json
     * @throws Exception
     * @throws \app\lib\exception\ParameterException
     */
    public function AttrValAdd(Request $request){
        (new AttrValAddValidate())->goCheck();
        try {
            $post  =  $post  = $request->post();
            $post['spec_item_id'] = (new SpecItem())->createSpecId("II");
            $new = [
                'spec_id'=>$post['spec_id'],
                'create_time'=>time(),
                'update_time'=>time(),
                'spec_item_id'=> $post['spec_item_id'],
                'sort'      =>$post['sort'],
                'spec_item_value'=>$post['spec_item_value']
            ];
            $result =(new SpecItem())->dataUpdate($new);
            if(!$result){
                throw new FailedException([ 'msg' => '新增属性集失败，请重试']);
            }
            return writeJson(200,[]);
        }catch (Exception $exception){
            throw $exception;
        }


    }
    /**
     * 属性值修改
     * spec_title
     * @param Request $request
     * @return \think\response\Json
     * @throws Exception
     * @throws \app\lib\exception\ParameterException
     */
    public function AttrValEdit(Request $request){
        (new AttrValAddValidate())->goCheck();
        (new IdMustPositive())->goCheck();
        try {
            $post = $request->post();
            Db::startTrans();
            $res = (new SpecItem())->dataUpdate($post);
            if(!$res){
                Db::rollback();
                throw new FailedException([ 'msg' => '修改属性集失败，请重试']);
            }
            Db::commit();
            return writeJson(200,[]);
        }catch (Exception $exception){
            throw $exception;
        }
    }
    /**
     * 删除 属性值
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function AttrValDelete(Request $request){
        (new IdMustPositive())->goCheck();
        $post  = $request->post();
        Db::startTrans();
        $result = (new \app\admin\model\SpecItem())->SoftDelete([['id','eq',$post['id']]]);
        if(!$result){
            Db::rollback();
            throw new FailedException([ 'msg' => '删除分类失败，请重试']);
        }
        Db::commit();
        return writeJson(200,[]);
    }







}